import dayjs from "dayjs";
import { Examplesave, ExampleDelete } from "@/api/teacher/example";
import { GetStoppage, GetStoppageSave } from "@/api/teacher/injurytime";
import { type PaginationProps } from "@pureadmin/table";
import { GetStuByClass } from "@/api/admin/teacher";
import { PlanGetPage, GetAllPlan } from "@/api/teacher/plan";
import { ClassGetAll } from "@/api/teacher/zsclass";
import { reactive, ref, onMounted, watch } from "vue";
import { ElMessage } from "element-plus";
import qs from "qs";
export function useExample() {
  const form = reactive({
    planId: "",
    classId: "",
    userId: ""
  });

  const ExampleDto = reactive({
    Id: "",
    Long: "",
    Str1: "",
    Money: "",
    RoleType: "",
    DateTime: "",
    Bool: ""
  });

  const classList = ref<any>([]);
  const planList = ref<any>([]);
  const stuList = ref<any>([]);
  const dataList = ref([]);
  const loading = ref(false);
  const PanelVisibleisshow = ref(false);
  const title = ref("");
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true
  });
  const columns: TableColumnList = [
    {
      label: "序号",
      type: "index",
      minWidth: 100
    },
    {
      label: "计划名称",
      prop: "planName",
      minWidth: 120
    },
    {
      label: "班级名称",
      prop: "className",
      minWidth: 150
    },
    {
      label: "学生账号",
      prop: "userId",
      minWidth: 120
    },
    {
      label: "登记时间",
      minWidth: 180,
      prop: "createTime",
      formatter: ({ createTime }) =>
        dayjs(createTime).format("YYYY-MM-DD HH:mm:ss")
    }
  ];

  function PanelVisible(row) {
    row === 0 ? (title.value = "添加") : (title.value = "修改");
    if (row) {
      ExampleDto.Bool = row.Bool;
      ExampleDto.DateTime = row.DateTime;
      ExampleDto.Id = row.Id;
      ExampleDto.Long = row.Long;
      ExampleDto.Money = row.Money;
      ExampleDto.RoleType = row.RoleType;
      ExampleDto.Str1 = row.Str1;
    } else {
      ExampleDto.Bool = "";
      ExampleDto.DateTime = "";
      ExampleDto.Id = "";
      ExampleDto.Long = "";
      ExampleDto.Money = "";
      ExampleDto.RoleType = "";
      ExampleDto.Str1 = "";
    }
    PanelVisibleisshow.value = true;
  }

  async function handleDelete(row) {
    await ExampleDelete(row);
    onSearch();
  }

  function handleSizeChange(val: number) {
    console.log(`${val} items per page`);
    onSearch();
  }

  function handleCurrentChange(val: number) {
    console.log(`current page: ${val}`);
    onSearch();
  }

  function handleSelectionChange(val) {
    console.log("handleSelectionChange", val);
  }
  //初始化下拉框数据
  async function initSelect() {
    //获取班级下拉框
    const response = await ClassGetAll();
    if (response.success == true) {
      classList.value = response.data;
    }
    //获取计划下拉框
    const { data, success } = await GetAllPlan();
    if (success) {
      planList.value = data;
    }
  }

  async function onSearch() {
    if (form.classId && form.planId) {
      const req = await GetStoppage({
        ...form,
        pageIndex: pagination.currentPage,
        pageCount: pagination.pageSize
      });
      dataList.value = req.data.list;
      pagination.total = req.data.totalCount;
    } else {
      ElMessage.warning("计划名称、班级名称不能为空");
    }
  }

  async function addExample() {
    const respone = await Examplesave(ExampleDto);
    if (respone.success) {
      onSearch();
      PanelVisibleisshow.value = false;
      ElMessage({
        message: "添加成功",
        type: "success"
      });
    } else {
      ElMessage({
        message: respone.msg,
        type: "success"
      });
    }
  }

  async function updateExample() {
    const req = await GetStoppageSave(form);
    if (req.success) {
      ElMessage.success(req.msg);
    } else {

      ElMessage.error(req.msg);
    }
    PanelVisibleisshow.value = false;
    onSearch();
  }

  const resetForm = formEl => {
    formEl.resetFields();
    dataList.value = []
  };
  //初始化计划、班级数据
  onMounted(() => {
    initSelect();
  });
  //获取操作用户下拉框数据
  watch(
    () => form.classId,
    async () => {
      if (form.classId) {
        const data = qs.stringify({
          classId: form.classId
        });
        const res = await GetStuByClass(data);
        stuList.value = res.data;
      } else {
        stuList.value = [];
      }
    }
  );
  return {
    form,
    loading,
    columns,
    dataList,
    pagination,
    ExampleDto,
    classList,
    planList,
    stuList,
    PanelVisibleisshow,
    title,
    onSearch,
    resetForm,
    PanelVisible,
    handleDelete,
    handleSizeChange,
    handleCurrentChange,
    handleSelectionChange,
    addExample,
    updateExample
  };
}
